home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / toolkit.jar / content / mozapps / profile / profileSelection.js < prev    next >
Encoding:
JavaScript  |  2007-11-13  |  9.2 KB  |  296 lines

  1. /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is Mozilla Communicator client code, released
  17.  * March 31, 1998.
  18.  *
  19.  * The Initial Developer of the Original Code is
  20.  * Netscape Communications Corporation.
  21.  * Portions created by the Initial Developer are Copyright (C) 1998-1999
  22.  * the Initial Developer. All Rights Reserved.
  23.  *
  24.  * Contributor(s):
  25.  *   Ben Goodger (03/01/00)
  26.  *   Seth Spitzer (28/10/99)
  27.  *   Dan Veditz <dveditz@netscape.com>
  28.  *   Benjamin Smedberg <bsmedberg@covad.net>
  29.  *
  30.  * Alternatively, the contents of this file may be used under the terms of
  31.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  32.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33.  * in which case the provisions of the GPL or the LGPL are applicable instead
  34.  * of those above. If you wish to allow use of your version of this file only
  35.  * under the terms of either the GPL or the LGPL, and not to allow others to
  36.  * use your version of this file under the terms of the MPL, indicate your
  37.  * decision by deleting the provisions above and replace them with the notice
  38.  * and other provisions required by the GPL or the LGPL. If you do not delete
  39.  * the provisions above, a recipient may use your version of this file under
  40.  * the terms of any one of the MPL, the GPL or the LGPL.
  41.  *
  42.  * ***** END LICENSE BLOCK ***** */
  43.  
  44. const C = Components.classes;
  45. const I = Components.interfaces;
  46.  
  47. const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
  48. const PromptService = "@mozilla.org/embedcomp/prompt-service;1";
  49.  
  50. var gDialogParams;
  51. var gProfileManagerBundle;
  52. var gBrandBundle;
  53. var gProfileService;
  54. var gPromptService;
  55.  
  56. function startup()
  57. {
  58.   try {
  59.     gDialogParams = window.arguments[0].
  60.       QueryInterface(I.nsIDialogParamBlock);
  61.  
  62.     gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
  63.  
  64.     gProfileManagerBundle = document.getElementById("bundle_profileManager");
  65.     gBrandBundle = document.getElementById("bundle_brand");
  66.  
  67.     gPromptService = C[PromptService].getService(I.nsIPromptService);
  68.  
  69.     document.documentElement.centerWindowOnScreen();
  70.  
  71.     var profilesElement = document.getElementById("profiles");
  72.  
  73.     var profileList = gProfileService.profiles;
  74.     while (profileList.hasMoreElements()) {
  75.       var profile = profileList.getNext().QueryInterface(I.nsIToolkitProfile);
  76.  
  77.       var listitem = profilesElement.appendItem(profile.name, "");
  78.  
  79.       var tooltiptext =
  80.         gProfileManagerBundle.getFormattedString("profileTooltip", [profile.name, profile.rootDir.path]);
  81.       listitem.setAttribute("tooltiptext", tooltiptext);
  82.       listitem.setAttribute("class", "listitem-iconic");
  83.       listitem.profile = profile;
  84.       try {
  85.         if (profile === gProfileService.selectedProfile) {
  86.           setTimeout(function(a) {
  87.             profilesElement.ensureElementIsVisible(a);
  88.             profilesElement.selectItem(a);
  89.           }, 0, listitem);
  90.         }
  91.       }
  92.       catch(e) { }
  93.     }
  94.  
  95.     var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
  96.     autoSelectLastProfile.checked = gProfileService.startWithLastProfile;
  97.     profilesElement.focus();
  98.   }
  99.   catch(e) {
  100.     window.close();
  101.     throw (e);
  102.   }
  103. }
  104.  
  105. function acceptDialog()
  106. {
  107.   var appName = gBrandBundle.getString("brandShortName");
  108.  
  109.   var profilesElement = document.getElementById("profiles");
  110.   var selectedProfile = profilesElement.selectedItem;
  111.   if (!selectedProfile) {
  112.     var pleaseSelectTitle = gProfileManagerBundle.getString("pleaseSelectTitle");
  113.     var pleaseSelect =
  114.       gProfileManagerBundle.getFormattedString("pleaseSelect", [appName]);
  115.     gPromptService.alert(window, pleaseSelectTitle, pleaseSelect);
  116.  
  117.     return false;
  118.   }
  119.  
  120.   var profileLock;
  121.  
  122.   try {
  123.     profileLock = selectedProfile.profile.lock({ value: null });
  124.   }
  125.   catch (e) {
  126.     var lockedTitle = gProfileManagerBundle.getString("profileLockedTitle");
  127.     var locked =
  128.       gProfileManagerBundle.getFormattedString("profileLocked2", [appName, selectedProfile.profile.name, appName]);
  129.     gPromptService.alert(window, lockedTitle, locked);
  130.  
  131.     return false;
  132.   }
  133.   gDialogParams.objects.insertElementAt(profileLock.nsIProfileLock, 0, false);
  134.  
  135.   gProfileService.selectedProfile = selectedProfile.profile;
  136.   updateStartupPrefs();
  137.  
  138.   gDialogParams.SetInt(0, 1);
  139.  
  140.   gDialogParams.SetString(0, selectedProfile.profile.name);
  141.  
  142.   return true;
  143. }
  144.  
  145. function exitDialog()
  146. {
  147.   updateStartupPrefs();
  148.   
  149.   return true;
  150. }
  151.  
  152. function updateStartupPrefs()
  153. {
  154.   var autoSelectLastProfile = document.getElementById("autoSelectLastProfile");
  155.   gProfileService.startWithLastProfile = autoSelectLastProfile.checked;
  156.  
  157.   /* Bug 257777 */
  158.   gProfileService.startOffline = document.getElementById("offlineState").checked;
  159. }
  160.  
  161. // handle key event on listboxes
  162. function onProfilesKey(aEvent)
  163. {
  164.   switch( aEvent.keyCode ) 
  165.   {
  166.   case KeyEvent.DOM_VK_DELETE:
  167.     ConfirmDelete();
  168.     break;
  169.   case KeyEvent.DOM_VK_F2:
  170.     RenameProfile();
  171.     break;
  172.   }
  173. }
  174.  
  175. function onProfilesDblClick(aEvent)
  176. {
  177.   if(aEvent.target.localName == "listitem")
  178.     document.documentElement.acceptDialog();
  179. }
  180.  
  181. // invoke the createProfile Wizard
  182. function CreateProfileWizard()
  183. {
  184.   window.openDialog('chrome://mozapps/content/profile/createProfileWizard.xul',
  185.                     '', 'centerscreen,chrome,modal,titlebar', gProfileService);
  186. }
  187.  
  188. /**
  189.  * Called from createProfileWizard to update the display.
  190.  */
  191. function CreateProfile(aProfile)
  192. {
  193.   var profilesElement = document.getElementById("profiles");
  194.  
  195.   var listitem = profilesElement.appendItem(aProfile.name, "");
  196.  
  197.   var tooltiptext =
  198.     gProfileManagerBundle.getFormattedString("profileTooltip", [aProfile.name, aProfile.rootDir.path]);
  199.   listitem.setAttribute("tooltiptext", tooltiptext);
  200.   listitem.setAttribute("class", "listitem-iconic");
  201.   listitem.profile = aProfile;
  202.  
  203.   profilesElement.ensureElementIsVisible(listitem);
  204.   profilesElement.selectItem(listitem);
  205. }
  206.  
  207. // rename the selected profile
  208. function RenameProfile()
  209. {
  210.   var profilesElement = document.getElementById("profiles");
  211.   var selectedItem = profilesElement.selectedItem;
  212.   if (!selectedItem) {
  213.     return false;
  214.   }
  215.  
  216.   var selectedProfile = selectedItem.profile;
  217.  
  218.   var oldName = selectedProfile.name;
  219.   var newName = {value: oldName};
  220.  
  221.   var dialogTitle = gProfileManagerBundle.getString("renameProfileTitle");
  222.   var msg =
  223.     gProfileManagerBundle.getFormattedString("renameProfilePrompt", [oldName]);
  224.  
  225.   if (gPromptService.prompt(window, dialogTitle, msg, newName, null, {value:0})) {
  226.     newName = newName.value;
  227.  
  228.     // User hasn't changed the profile name. Treat as if cancel was pressed.
  229.     if (newName == oldName)
  230.       return false;
  231.  
  232.     try {
  233.       selectedProfile.name = newName;
  234.     }
  235.     catch (e) {
  236.       var alTitle = gProfileManagerBundle.getString("profileNameInvalidTitle");
  237.       var alMsg = gProfileManagerBundle.getFormattedString("profileNameInvalid", [newName]);
  238.       gPromptService.alert(window, alTitle, alMsg);
  239.       return false;
  240.     }
  241.  
  242.     selectedItem.label = newName;
  243.     var tiptext = gProfileManagerBundle.
  244.                   getFormattedString("profileTooltip",
  245.                                      [newName, selectedProfile.rootDir.path]);
  246.     selectedItem.setAttribute("tooltiptext", tiptext);
  247.  
  248.     return true;
  249.   }
  250.  
  251.   return false;
  252. }
  253.  
  254. function ConfirmDelete()
  255. {
  256.   var deleteButton = document.getElementById("delbutton");
  257.   var profileList = document.getElementById( "profiles" );
  258.  
  259.   var selectedItem = profileList.selectedItem;
  260.   if (!selectedItem) {
  261.     return false;
  262.   }
  263.  
  264.   var selectedProfile = selectedItem.profile;
  265.   var deleteFiles = false;
  266.  
  267.   if (selectedProfile.rootDir.exists()) {
  268.     var dialogTitle = gProfileManagerBundle.getString("deleteTitle");
  269.     var dialogText =
  270.       gProfileManagerBundle.getFormattedString("deleteProfileConfirm",
  271.                                                [selectedProfile.rootDir.path]);
  272.  
  273.     var buttonPressed = gPromptService.confirmEx(window, dialogTitle, dialogText,
  274.                           (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_0) +
  275.                           (gPromptService.BUTTON_TITLE_CANCEL * gPromptService.BUTTON_POS_1) +
  276.                           (gPromptService.BUTTON_TITLE_IS_STRING * gPromptService.BUTTON_POS_2),
  277.                           gProfileManagerBundle.getString("dontDeleteFiles"),
  278.                           null,
  279.                           gProfileManagerBundle.getString("deleteFiles"),
  280.                           null, {value:0});
  281.     if (buttonPressed == 1)
  282.       return false;
  283.  
  284.     if (buttonPressed == 2)
  285.       deleteFiles = true;
  286.   }
  287.   
  288.   selectedProfile.remove(deleteFiles);
  289.   profileList.removeChild(selectedItem);
  290.   if (profileList.firstChild != undefined) {
  291.     profileList.selectItem(profileList.firstChild);
  292.   }
  293.  
  294.   return true;
  295. }
  296.